#!/usr/bin/python
import re
import string
import sys
import time
from xml.sax import saxutils


class Question:
    text = None
    answers = []
    rightAnswer = None
    difficulty = None

    def __init__(self):
        self.text = None
        self.answers = []
        self.rightAnswer = None
        self.difficulty = None


results = []

for fname in sys.argv[1:]:
    fh = open(fname)

    tex = fh.readlines()

    q = None
    qEnded = 0
    aStarted = 0
    for line in tex:

        # create Question and set text to first line (rest of lines will be added
        # later)
        if q == None and re.search('\\\\bq{[0-9]*?}', line) != None and \
                re.search('^\s*%', line) == None:
            dif = re.sub('.*\\\\bq{([0-9]*?)}.*\n', '\\1', line)
            newline = re.sub('\\\\bq{[0-9]*?}', '', line)
            newline = re.sub('\\\\label{.*?} ', '', newline)

            q = Question()
            q.text = newline
            q.difficulty = int(dif)
            continue

        # add rest of question text
        if q != None and re.search('\\\\eq', line) == None and not qEnded:
            q.text = q.text + line
            continue

        # question text ends when we find \eq
        if q != None and re.search('\\\\eq', line):
            qEnded = 1
            continue

        # answers start when we find \begin{answers}
        if q != None and qEnded and re.search('\\\\begin{answers}', line) != None:
            aStarted = 1
            continue
        
        if q != None and aStarted and re.search('\\\\end{answers}', line) != None:
            aStarted = 0
            qEnded = 0
            cnt = 0
            tmparr = []
            for ans in q.answers:
                if ans.find('\\label{q\\theqnum:a}') != -1:
                    q.rightAnswer = cnt
                    ans = ans.replace('\\label{q\\theqnum:a}', '')
                ans = ans.lstrip()
                ans = ans.rstrip()
                tmparr.append(ans)
                cnt = cnt + 1
            
            q.text = q.text.lstrip()
            q.text = q.text.rstrip()
            q.answers = tmparr
            results.append(q)
            q = None
            continue

        if q != None and aStarted and re.search('\\\\item ', line) == None:
            q.answers[len(q.answers)-1] = q.answers[len(q.answers)-1] + '\n' + line
            continue
        
        if q != None and aStarted and re.search('\\\\item ', line) != None:
            ans = re.sub('\\\\item ', '', line)
            ans = re.sub('\n','', ans)

            q.answers.append(ans)
            continue




print '<?xml version="1.0" encoding="UTF-8"?>'
print '<questions>'

for q in results:

    print '<question difficulty="%d">' % (q.difficulty)
    print '<text>%s</text>' % (saxutils.escape(q.text))
    print '<answers>'
    num = 0
    for a in q.answers:
        print '<answer correct="%d">%s</answer>' % (num == q.rightAnswer,
                saxutils.escape(a))
        num = num + 1
    print '</answers>'
    print '</question>\n'

print '</questions>'
